package com.frank.oj.service.entity.contest.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frank.model.entity.contest.Contest;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.mapper.ContestMapper;
import com.frank.oj.model.vo.ContestRegisterCountVO;
import com.frank.oj.model.vo.ContestVO;
import com.frank.oj.service.entity.contest.ContestEntityService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author frank
 * @Date 2023/12/21
 */
@Service
public class ContestEntityServiceImpl extends ServiceImpl<ContestMapper, Contest> implements ContestEntityService {
    @Resource
    private ContestMapper contestMapper;

    /**
     * 获取最近14天的比赛信息列表
     */
    @Override
    public List<ContestVO> getWithinNext14DaysContest() {
        List<ContestVO> contestList = contestMapper.getWithinNext14DaysContest();
        setRegisterCount(contestList);
        return contestList;

    }

    @Override
    public CommonResult<IPage<ContestVO>> getContestList(Integer limit, Integer currentPage, Integer status, Integer type, String keyword) {

        Page<ContestVO> page = new Page<>(currentPage, limit);

        List<ContestVO> contestList = contestMapper.getContestList(page, status, type, keyword);
        setRegisterCount(contestList);

        return CommonResult.successResponse(page.setRecords(contestList));
    }

    @Override
    public ContestVO getContestInfoById(Long cid) {
        List<Long> cidList = Collections.singletonList(cid);
        ContestVO contestVo = contestMapper.getContestInfoById(cid);
        if (contestVo!=null){
            List<ContestRegisterCountVO> contestRegisterCount = contestMapper.getContestRegisterCount(cidList);
            if (!CollectionUtils.isEmpty(contestRegisterCount)) {
                ContestRegisterCountVO contestRegisterCountVO = contestRegisterCount.get(0);
                contestVo.setCount(contestRegisterCountVO.getCount());
            }
        }
        return contestVo;
    }

    /**
     * 设置比赛报名人数
     *
     * @param contestList 比赛信息列表
     */
    private void setRegisterCount(List<ContestVO> contestList) {
        List<Long> cids = contestList.stream().map(ContestVO::getId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(cids)) {
            List<ContestRegisterCountVO> contestRegisterCounts = contestMapper.getContestRegisterCount(cids);

            for (ContestRegisterCountVO contestRegisterCount : contestRegisterCounts) {
                for (ContestVO contestVO : contestList) {
                    if (contestRegisterCount.getCid().equals(contestVO.getId())) {
                        contestVO.setCount(contestRegisterCount.getCount());
                        break;
                    }
                }
            }
        }
    }
}
